<?php
require_once("header.php");


$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
$ip = getIP();
$country = geoip_country_code_by_addr($gi, getIP());
if(isset($_GET['id']) && isset($_GET['f']) && isset($_GET['r']))
{

    $file_id = safeGet($_GET['f']);
	$file_id = getFileIdByFileCode($file_id);
	$uid = getUploaderId($file_id);
    $offer_id = makesafe(safeGet($_GET['id']));
	$sql1 = mysql_query("SELECT *  FROM offers  WHERE id = '$offer_id' AND (countries LIKE '%".$country."%' OR countries = 'All') AND (`hits` < `limit` OR `limit` = 0) AND active = 1 LIMIT 1");
	if(!mysql_num_rows($sql1))
	{
        die("Invalid Survey!");
		return;
	}
	
	
	

	
	$row = mysql_fetch_object($sql1);
	$link = $row->link;
	$network = $row->network;
	$campaignId = stripslashes($row->campaign_id);
	
	
	$payout = $row->credits;

	
	

	
	$credits = sprintf($payout * (OFFER_RATE / 100));	
	$ref_credits = sprintf($payout * (REFERRAL_RATE / 100));			

	
	
	
	$type = $row->type;
	$network = $row->network;
	$offername = makesafe(stripslashes($row->name));
	$hash = makesafe(safeGet($_GET['r'])); //md5(uniqid().uniqid().rand());
	$creditMode = 'Default';
	
	
  if(mysql_num_rows(mysql_query("SELECT id FROM offer_process WHERE code = '$hash' AND  offer_id = '$offer_id' AND status = '1'")))
  die("you have already completed this offer, please try other");	
	
  @mysql_query("DELETE FROM offer_process WHERE code = '$hash' AND offer_id = '$offer_id' AND status = '0'");
	
	//Update uhits in offers table
	//mysql_query("UPDATE offers SET uhits=uhits+1 WHERE uid = $uid AND id = '$offer_id'");
    //insert offer into process 


	


	mysql_query("INSERT INTO offer_process VALUES(NULL, '$campaignId', '$offername', $uid, '$hash', 0, NOW(), '$ip', '$credits', '$ref_credits', '$network', '$offer_id', '$file_id', '$creditMode', '$country', '0', '1')") or die("Error! Please try later or contact admin to report this error ");
	
	$username = getUserById($uid);
	if(!$username)
	{
	   die("An error occured, please try later, or contact admin to report this error.");
	}
	
	//update clicks count
	@mysql_query("UPDATE offers SET hits = hits+1 WHERE campaign_id = '$campaignId' AND network = '$network' LIMIT 1");
	//update epc
    setEpc($campaignId, $network);
	

	switch($network)
	{
	
		
	    case 'Adscendmedia':
	    case 'CPALead':
		$q = "$uid-$hash";
		break;	

	    case 'Maxbounty':
		$q = "&s1=$uid&s2=$hash";
		break;	

		
		case 'Adgatemedia':
		case 'dlg':
		$q = "&aff_sub=$uid&aff_sub2=$hash&sub=$uid&sub2=$hash";
		break; 
		
                case 'ClearlinkMedia':
		case 'clearlinkmedia':
		$q = "&aff_sub=$uid&aff_sub2=$hash&sub=$uid&sub2=$hash";
		break; 

		case 'BlamAds':
		$q = "&aff_sub=$uid&aff_sub2=$hash&sub_id=$uid&sub_id2=$hash";
		break; 
		
		case 'Adworkmedia':
		case 'adworkmedia':		
		$q = "&sid=$uid&sid2=$hash";
		break; 	
		
		
		case 'Adnooka':
		case 'adnooka':
		$q = "&sid=$uid&sid2=$hash";
		break; 		
		
		
		case 'adshiftmedia':		
		$q = "$uid&sid2=$hash";
		break; 						
		
		
	}
	

		$offerLink = $link.$q;
		$offerLink = str_replace("&amp;", "&", $offerLink);


	
	header("location: $offerLink");
	
	

}



?>